import {
	login,
	logout,
	getInfo
} from '@/api/index.js'
import {
	getToken,
	setToken,
	removeToken
} from '@/utils/auth'
// import router, { resetRouter } from '@/router'

const state = {
	token: getToken(),
	nickname: '',
	head_portrait: '',
	country: '',
	email: '',
	id: 0,
	extension_code: '',
	phone: '',
	prefix: '',
	type: 0,
	totalAssets: 0,
	name: '',
	btc: 0,
	eth: 0,
	usdt: 0,
	usdt_lock: 0,
	review_status: 0,
	account_number: ''
}

const mutations = {
	SET_TOKEN: (state, token) => {
		state.token = token
	},
	SET_USDT: (state, usdt) => {
		state.usdt = usdt
	},
	SET_NICKNAME: (state, nickname) => {
		state.nickname = nickname
	},
	SET_HEAD_PORTRAIT: (state, head_portrait) => {
		state.head_portrait = head_portrait
	},
	SET_COUNTRY: (state, country) => {
		state.country = country
	},
	SET_EMAIL: (state, email) => {
		state.email = email
	},
	SET_ID: (state, id) => {
		state.id = id
	},
	SET_EXTENSION_CODE: (state, extension_code) => {
		state.extension_code = extension_code
	},
	SET_PHONE: (state, phone) => {
		state.phone = phone
	},
	SET_PREFIX: (state, prefix) => {
		state.prefix = prefix
	},
	SET_TYPE: (state, type) => {
		state.type = type
	},
	SET_TOTALASSETS: (state, totalAssets) => {
		state.totalAssets = totalAssets
	},
	SET_NAME: (state, name) => {
		state.name = name
	},
	SET_BTC: (state, btc) => {
		state.btc = btc
	},
	SET_ETH: (state, eth) => {
		state.eth = eth
	},
	SET_USDT_LOCK: (state, usdt_lock) => {
		state.usdt_lock = usdt_lock
	},
	SET_REVIEW_STATUS: (state, review_status) => {
		state.review_status = review_status
	},
	SET_ACCOUNT_NUMBER: (state, account_number) => {
		state.account_number = account_number
	},
}

const actions = {
	// user login
	login({
		commit
	}, userInfo) {
		const {
			username,
			password,
			logintype,
			prefix
		} = userInfo
		return new Promise((resolve, reject) => {
			login({
				username: username.trim(),
				password: password,
				logintype: logintype,
				prefix: prefix
			}).then(response => {
				if (response.error) return resolve({
					data: false
				});
				let data = response.res
				commit('SET_TOKEN', data)
				setToken(data)
				resolve({
					data: true
				})
			})
		})
	},

	getInfo({
		commit,
		state
	}) {
		return new Promise((resolve, reject) => {
			getInfo({
				token: state.token
			}).then(response => {
				if (response.error) return reject();
				let data = response.res
				commit('SET_ID', data.id)
				commit('SET_USDT', data.usdt)
				commit('SET_NICKNAME', data.nickname)
				commit('SET_COUNTRY', data.country)
				commit('SET_EMAIL', data.email)
				commit('SET_EXTENSION_CODE', data.extension_code)
				commit('SET_PHONE', data.phone)
				commit('SET_HEAD_PORTRAIT', data.head_portrait)
				commit('SET_PREFIX', data.prefix)
				commit('SET_NAME', data.name)
				commit('SET_TYPE', data.type)
				commit('SET_BTC', data.btc)
				commit('SET_ETH', data.eth)
				commit('SET_USDT_LOCK', data.usdt_lock)
				commit('SET_REVIEW_STATUS', data.review_status)
				commit('SET_ACCOUNT_NUMBER', data.account_number)
				resolve(data)
			})
		})
	},

	// user logout
	logout({
		commit
	}) {
		return new Promise((resolve, reject) => {
			// logout().then(res => {
			commit('SET_TOKEN', '')
			commit('SET_ID', 0)
			commit('SET_NICKNAME', '')
			commit('SET_COUNTRY', '')
			commit('SET_EMAIL', '')
			commit('SET_EXTENSION_CODE', '')
			commit('SET_PHONE', '')
			commit('SET_HEAD_PORTRAIT', '')
			commit('SET_PREFIX', '')
			commit('SET_NAME', '')
			commit('SET_TYPE', 0)
			commit('SET_BTC', 0)
			commit('SET_ETH', 0)
			commit('SET_USDT', 0)
			commit('SET_USDT_LOCK', 0)
			commit('SET_REVIEW_STATUS', 0)
			removeToken()
			resolve()
			// }).catch(() => {reject()})
		})
	}
}

export default {
	namespaced: true,
	state,
	mutations,
	actions
}
